package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.chainS.inventoryFlow.ChainInOutStock;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCust;
import com.onlineMIS.ORM.entity.headQ.finance.ChainAcctFlow;
import com.onlineMIS.ORM.entity.headQ.report.HeadQDealingsReportVO;
import com.onlineMIS.common.Common_util;

@Repository
public class ChainAcctFlowDaoImpl  extends BaseDAO<ChainAcctFlow> {

	
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public double getAcctFlowByCustId(int custId){
		DetachedCriteria criteria = DetachedCriteria.forClass(ChainAcctFlow.class);

		criteria.add(Restrictions.eq("custId", custId));
		
		criteria.setProjection(Projections.sum("acctAmt"));
		
		List<Object> result = getByCriteriaProjection(criteria, false);

        return Common_util.getProjectionDoubleValue(result);
	}
	
	/**
	 * 根据ids获取客户欠款信息
	 * @param custIds
	 * @return
	 */
	public Map<Integer, Double> getAcctFlowAmtByIds(List<Integer> custIds){
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		String sql = "select custId,sum(acctAmt) from ChainAcctFlow s1 where custId in"+Common_util.parseList2SQLString(custIds)+" group by custId";

		List<Object> values = this.executeHQLSelect(sql.toString(), null, null, true);
		if (values.size() != 0) {
			for (Object record : values) {		
				Object[] ob = (Object[]) record;
				int custId = Common_util.getInt(ob[0]);
				double accmt = Common_util.getDouble(ob[1]);
				map.put(custId, accmt);
			}
		}
        return map;
	}
	
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public double getAccumulateAcctFlowBefore(int clientId, Date date){
		DetachedCriteria criteria = DetachedCriteria.forClass(ChainAcctFlow.class);

		criteria.add(Restrictions.eq("custId", clientId));
		
		criteria.add(Restrictions.lt("date", date));
		
		criteria.setProjection(Projections.sum("acctAmt"));
		
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionDoubleValue(result);
	}
	
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public double getAccumulateAcctFlowBefore(List<Integer> clientIds, Date date){
		DetachedCriteria criteria = DetachedCriteria.forClass(ChainAcctFlow.class);

		criteria.add(Restrictions.in("custId", clientIds));
		
		criteria.add(Restrictions.lt("date", date));
		
		criteria.setProjection(Projections.sum("acctAmt"));
		
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionDoubleValue(result);
	}
	
	/**
	 * get the accumulate acct flow
	 * @return
	 */
	public Map<Integer, Double> getAcctFlowAmt(List<Integer> custIds,Date date){
		Map<Integer, Double> map = new HashMap<Integer, Double>();
		String sql = "select custId,sum(acctAmt) from ChainAcctFlow s1 where s1.date < ?  and  custId in"+Common_util.parseList2SQLString(custIds)+" group by custId";
		// 根据条件补充部分
		List<Object> list = new ArrayList<Object>();

		list.add(date);
		List<Object> values = this.executeHQLSelect(sql.toString(), list.toArray(), null, true);
		if (values.size() != 0) {
			for (Object record : values) {		
				Object[] ob = (Object[]) record;
				int custId = Common_util.getInt(ob[0]);
				double accmt = Common_util.getDouble(ob[1]);
				map.put(custId, accmt);
			}
		}
        return map;
	}

	/**
	 * 为往来账户，上期欠款赋值
	 * @param custs
	 * @param startDate
	 * @return
	 */
	public List<HeadQDealingsReportVO> setCtpDebt(List<HeadQCust> custs, java.sql.Date startDate) {
		List<HeadQDealingsReportVO> list = new ArrayList<HeadQDealingsReportVO>();
		List<Integer> custIds = new ArrayList<Integer>();
		for (HeadQCust headQCust : custs) {
			custIds.add(headQCust.getId());
		}
		Map<Integer, Double> map = getAcctFlowAmt(custIds,startDate);
					
		for (HeadQCust headQCust : custs) {
			//1.期初欠款
			double initialAcct = headQCust.getInitialAcctBalance();
			double debt = 0;
			if (map.get(headQCust.getId()) != null) {
				debt = map.get(headQCust.getId());
			}	
		    //2.当前欠款
			double currentFinance = initialAcct + debt;
			currentFinance = Common_util.roundDouble(currentFinance, 2);
			HeadQDealingsReportVO vo = new HeadQDealingsReportVO();
			vo.setClientId(headQCust.getId());
			vo.setClientName(headQCust.getName());
			vo.setCtpDebt(currentFinance);
			list.add(vo);
					
		}
		return list;
	}

	/**
	 * 根据时间获取金额合计
	 * @param formStartDate
	 * @return
	 */
	public double getBeforeDateSumAcctAmt(Date date) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ChainAcctFlow.class);
		
		criteria.add(Restrictions.lt("date", date));
		
		criteria.setProjection(Projections.sum("acctAmt"));
		
		List<Object> result = getByCriteriaProjection(criteria, true);

        return Common_util.getProjectionDoubleValue(result);
	}



}
